package zhang.lao.service.console.base;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import zhang.lao.build.kit.LogKit;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.tool.BeanMapTool;
import zhang.lao.build.tool.ListUtils;
import zhang.lao.dao.base.*;
import zhang.lao.build.tool.LzStringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import zhang.lao.build.mybatis.jdbc.auto.tool.ControllerQueryTool;
import zhang.lao.pojo.console.ConsoleContext;
import zhang.lao.pojo.console.req.BootStrapGridReq;
import zhang.lao.pojo.console.resp.BootStrapGridResp;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.pojo.console.resp.HttpResult;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import zhang.lao.build.tool.UUIDTool;
import zhang.lao.service.console.MachineService;
import zhang.lao.service.sequence.SequenceTool;

/**
* 
* @author 
* @create 
**/
 @Service
public class WnMachineService{
	@Resource
	private WnMachineDao wnMachineDao;
	@Resource
	private WnFarmDao wnFarmDao;
	@Resource
	private WnAppUserDao wnAppUserDao;
	@Resource
	private WnMachineDeviceLinkDao wnMachineDeviceLinkDao;
	@Resource
	private WnMachineDeviceDao wnMachineDeviceDao;
	@Resource
	private SequenceTool sequenceTool;
	@Resource
	private WnMachineToolLinkDao wnMachineToolLinkDao;
	@Resource
	private WnMachineToolDao wnMachineToolDao;
	@Resource
	private WnMachineAppUserDao wnMachineAppUserDao;

	@Resource
	private MachineService machineService;
	public String add(){
		return "console/wnMachine/wnMachine_form";
	}

	public String edit(ModelMap modelMap,java.lang.String id){
		WnMachine wnMachine = wnMachineDao.selectByPrimaryKey(id);
		modelMap.put("wnMachine",wnMachine );
		WnMachineAppUser machineAppUser = machineService.getMachineAppUser(wnMachine.getId());
		modelMap.put("appUserId",machineAppUser.getAppUserId());
		return "console/wnMachine/wnMachine_form";
	}


	public String list(){
		return "console/wnMachine/wnMachine_table";
	}

	public BootStrapGridResp json(BootStrapGridReq bootGridReq, ConsoleContext consoleContext){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
		WnMachineExample wnMachineExample = new WnMachineExample();
        ControllerQueryTool.setWnMachineCriteria(bootGridReq.getQuery(),wnMachineExample.createCriteria()).andFarmIdIn(consoleContext.getFarms());
		List<WnMachine> wnMachineList = wnMachineDao.selectByExample(wnMachineExample);
		List<JSONObject> list = BeanMapTool.objectsToJsonObjects(wnMachineList);
		if(list!=null){
			for (JSONObject jsonObject : list) {
				jsonObject.put("deviceNumberGt06","未绑定");
				jsonObject.put("deviceNumberAuto","未绑定");
				jsonObject.put("machineTool","未绑定");
				jsonObject.put("farmName",wnFarmDao.selectByPrimaryKey(jsonObject.getString("farmId")).getName());


				WnMachineAppUser wnMachineAppUser = machineService.getMachineAppUser(jsonObject.getString("id"));
				jsonObject.put("userPhoneNumber","未绑定");
				jsonObject.put("userName","未绑定");
				if(StringUtils.isNotBlank(wnMachineAppUser.getAppUserId())){
					WnAppUser wnAppUser = wnAppUserDao.selectByPrimaryKey(wnMachineAppUser.getAppUserId());
					if(wnAppUser!=null){
						jsonObject.put("userPhoneNumber",wnAppUser.getPhoneNumber());
						jsonObject.put("userName",wnAppUser.getNickName());
					}
				}


				WnMachineDeviceLinkExample wnMachineDeviceLinkExample = new WnMachineDeviceLinkExample();
				wnMachineDeviceLinkExample.createCriteria().andMachineIdEqualTo(jsonObject.getString("id"));
				List<WnMachineDeviceLink> wnMachineDeviceLinks = wnMachineDeviceLinkDao.selectByExample(wnMachineDeviceLinkExample);
				if(ListUtils.isNotEmpty(wnMachineDeviceLinks)){
					for (WnMachineDeviceLink wnMachineDeviceLink : wnMachineDeviceLinks) {
						WnMachineDevice wnMachineDevice = wnMachineDeviceDao.selectByPrimaryKey(wnMachineDeviceLink.getDeviceId());
						if(wnMachineDevice.getDeviceType().equals("gt06")){
							jsonObject.put("deviceNumberGt06",wnMachineDevice.getDeviceNumber() );

						}else{
							jsonObject.put("deviceNumberAuto",wnMachineDevice.getDeviceNumber() );
						}

					}
				}

				WnMachineToolLinkExample wnMachineToolLinkExample = new WnMachineToolLinkExample();
				wnMachineToolLinkExample.createCriteria().andMachineIdEqualTo(jsonObject.getString("id"));
				List<WnMachineToolLink> wnMachineToolLinks = wnMachineToolLinkDao.selectByExample(wnMachineToolLinkExample);
				if(ListUtils.isNotEmpty(wnMachineToolLinks)){
					for (WnMachineToolLink wnMachineToolLink : wnMachineToolLinks) {
						String machineToolId = wnMachineToolLink.getMachineToolId();
						WnMachineTool wnMachineTool = wnMachineToolDao.selectByPrimaryKey(machineToolId);
						jsonObject.put("machineTool",wnMachineTool.getToolNumber());
						jsonObject.put("machineToolType",wnMachineTool.getType() );
						jsonObject.put("machineToolFk",wnMachineTool.getBreadth() );
					}
				}


			}
		}
		return new BootStrapGridResp(page.getTotal(),list);
	}

	public HttpResult save(String formObjectJson,String appUserId){
		WnMachine wnMachine= JSON.parseObject(formObjectJson,WnMachine.class);
			java.lang.String id=wnMachine.getId();
		if (id!=null) {
			wnMachineDao.updateByPrimaryKeySelective(wnMachine);
		}else{
			wnMachine.setId(UUIDTool.getUUID());
			wnMachine.setMachineNumber(sequenceTool.getSequence("MACHINE_NUMBER")+"");
			wnMachineDao.insertSelective(wnMachine);

		}
		if(StringUtils.isNotBlank(appUserId)){
			linkAppUser(wnMachine.getId(),appUserId);
		}
		return HttpResultUtil.buildSuccess(wnMachine.getId());

	}

	@Transactional
	public void linkAppUser(String machineId,String appuserId){
		if(StringUtil.isNotEmpty(appuserId)){
			WnMachineAppUserExample wnMachineAppUserExample = new WnMachineAppUserExample();
		    wnMachineAppUserExample.createCriteria().andAppUserIdEqualTo(appuserId);
			int i = wnMachineAppUserDao.deleteByExample(wnMachineAppUserExample);
			WnMachineAppUser wnMachineAppUser = new WnMachineAppUser();
			wnMachineAppUser.setAppUserId(appuserId);
			wnMachineAppUser.setCreateTime(new Date());
			wnMachineAppUser.setId(UUIDTool.getUUID());
			wnMachineAppUser.setMachineId(machineId);
			wnMachineAppUser.setUpdateTime(new Date());
			wnMachineAppUserDao.insert(wnMachineAppUser);

		}

	}
	@Transactional
	public HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
			WnMachineDeviceLinkExample wnMachineDeviceLinkExample = new WnMachineDeviceLinkExample();
			wnMachineDeviceLinkExample.createCriteria().andMachineIdEqualTo(id);
			wnMachineDeviceLinkDao.deleteByExample(wnMachineDeviceLinkExample);
			WnMachineToolLinkExample wnMachineToolLinkExample = new WnMachineToolLinkExample();
			wnMachineDeviceLinkExample.createCriteria().andMachineIdEqualTo(id);
			wnMachineToolLinkDao.deleteByExample(wnMachineToolLinkExample);
			wnMachineDao.deleteByPrimaryKey(java.lang.String.valueOf(id));
		}
		return HttpResultUtil.buildSuccess();
	}
}
